package praktikumid.w10.p2;

import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;

/**
 * A concrete shape - oval. It has center
 * and x,y radiuses.
 * @author Ago
 *
 */
public class Oval extends Shape {

	/**
	 * Center point of the oval.
	 */
	private Point center;
	/**
	 * Horizontal radius (x-axis).
	 */
	private int rx = 0;
	/**
	 * Vertical radius (y-axis).
	 */
	private int ry = 0;
	
	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		if (center != null) {
			// we need min to find out the leftmost
			int leftX = Math.min(center.x - rx, center.x + rx);
			// and uppermost coordinates
			// (here the name of the variable is not the best,
			// it could rather be up or something like that
			// or even minY).
			int leftY = Math.min(center.y - ry, center.y + ry);
			g.drawOval(leftX, leftY, 
					// width (and height) is 2 radiuses
					2 * Math.abs(rx), 2 * Math.abs(ry));
		}
	}

	
	@Override
	public void drag(MouseEvent e) {
		// when we start dragging, we need
		// to set center
		if (center == null)	{
			center = e.getPoint();
		} else {
			// if center is already fixed,
			// we can just modify the radiuses
			rx = e.getPoint().x - center.x;
			ry = e.getPoint().y - center.y;
		}
	}

	@Override
	public void click(MouseEvent e) {
		// if clicked, we can finish this shape
		// Note that click only occurs
		// when mouse press and release
		// takes place without moving the 
		// cursor between those events.
		// Otherwise, it's a drag event
		// and click is not happening.
		isFinished = true;
	}

	@Override
	public void release(MouseEvent e) {
		// mouse release
		// will be called before
		// mouse click
		// therefore we set center
		// here if center is null.
		if (center == null) {
			System.out.println("set center");
			center = e.getPoint();
		} else {
			// if we would always set finished
			// to true here, it would
			// call "click" event later on
			// and our implementation in DrawPanwl
			// would create a new object already.
			// Instead, if the use only clicked and
			// didn't drag (we know that
			// because center is null)
			// we have to set shape status
			// to finished in click method,
			// otherwise (if center != null,
			// we have dragged) we can set
			// it here (click won't be called)
			isFinished = true;
		}
		
	}


	@Override
	public int getDistance(Point p) {
		// for the distance, we use distance
		// from the center,
		// and compare it with radiuses.
		// In case of circle (only on radius)
		// it would be enough to compare
		// distance from the center and radius.
		// Here I used some heuristics
		// and compare distance with average radius.
		int d = (int) p.distance(center);
		return d - (rx + ry) / 2;
	}
	
	@Override
	public void move(int dx, int dy) {
		// we just need to move the center
		center.x = center.x + dx;
		center.y = center.y + dy;
	}

}